//
//  M3u8ResourceLoader.m
//  m3u8Player
//
//  Created by 缴天朔 on 2021/8/17.
//

#import "M3u8ResourceLoader.h"
#import <UIKit/UIKit.h>

@interface M3u8ResourceLoader(){
    NSString *m3u8_url_vir;
    NSString *m3u8_url;
}

@end

@implementation M3u8ResourceLoader

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    return [M3u8ResourceLoader shared];
}

-(instancetype)copyWitjZone:(NSZone *)zone{
    return [M3u8ResourceLoader shared];
}

-(instancetype)mutableCopyWithZone:(NSZone *)zone{
    return [M3u8ResourceLoader shared];
}

+(M3u8ResourceLoader *)shared{
    static M3u8ResourceLoader *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:nil] init];
    });
    return instance;
}

-(M3u8ResourceLoader *)init{
    self = [super init];
    m3u8_url_vir = @"m3u8Scheme://abcd.m3u8";
    return self;
}

/**
 拦截代理方法
 true表示：系统暂时不要播放，等待通知，才可以继续（相当于系统进程被阻断，直到收到了某些消息，才能继续运行）
 false表示：直接播放，不需要等待
 */
-(BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest{
    //获取到拦截的链接url
    NSString *aurl = [[[loadingRequest request] URL] absoluteString];
    
    if ([aurl hasSuffix: @".ts"]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //检查本地是否存在请求文件
            NSURLComponents *components = [[NSURLComponents alloc] initWithString:aurl];
            NSString *fileName = components.path;
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]  stringByAppendingString:fileName];
            NSFileManager *fm = [[NSFileManager alloc] init];
            while (![fm fileExistsAtPath:path]);
            //更改请求链接至有效链接
            NSString *newUrl = [aurl stringByReplacingOccurrencesOfString: @"rdtp" withString: @"http"];
            NSURL *url = [[NSURL alloc] initWithString: newUrl];
            NSLog(@"%@",[NSThread currentThread]);
            
            if (url) {
                NSURLRequest *redirect = [[NSURLRequest alloc] initWithURL: url];
                [loadingRequest setRedirect: redirect];
                [loadingRequest setResponse: [[NSHTTPURLResponse alloc] initWithURL: [redirect URL] statusCode: 301 HTTPVersion: nil headerFields: nil]];
                [loadingRequest finishLoading];
            } else {
                [self finishLoadingError: loadingRequest];
            }
        });
        return true;
    }
    
    if ([aurl isEqualToString: m3u8_url_vir]) {
            NSData *data = [self M3u8Request: m3u8_url];
            if (data) {
                    NSString *m3u8String = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
//                    m3u8String = [m3u8String stringByReplacingOccurrencesOfString:@"rdtp" withString:@"http"];
                    NSData *data = [m3u8String dataUsingEncoding: NSUTF8StringEncoding];
                    [[loadingRequest dataRequest] respondWithData: data];
                    [loadingRequest finishLoading];
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self finishLoadingError: loadingRequest];
                });
            }
        return true;
    }
    
    
    
    return false;
}


-(void)finishLoadingError:(AVAssetResourceLoadingRequest *)loadingRequest{
    [loadingRequest finishLoadingWithError:[[NSError alloc] initWithDomain:NSURLErrorDomain code:400 userInfo:nil]];
}

- (NSData *)M3u8Request: (NSString *)url {
    
    static NSData *result = NULL;
    NSError *error = nil;
    NSStringEncoding encoding = 0;
    NSString *urlContent = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url] encoding:encoding error:&error];
    urlContent = [urlContent stringByReplacingOccurrencesOfString:@"http" withString:@"rdtp"];
    result = [urlContent dataUsingEncoding: NSUTF8StringEncoding];

    return result;
}



- (AVPlayerItem *)playItemWith: (NSString *)url {
    m3u8_url = url;
    AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL: [[NSURL alloc] initWithString: m3u8_url_vir] options: nil];
    [[urlAsset resourceLoader] setDelegate: self queue: dispatch_get_main_queue()];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset: urlAsset];
    [item setCanUseNetworkResourcesForLiveStreamingWhilePaused: YES];
    return item;
}


@end
